package com.daka.controller;

import com.daka.entity.*;
import com.daka.enumerate.StatusCode;
import com.daka.service.*;
import com.daka.service.qnzyz.ConfigUtil;
import com.daka.tools.Page;
import com.daka.tools.StrUtils;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/8/5 0003
 * Time: 9:28
 */

@Controller
@RequestMapping("/activity")
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityAddressService activityAddressService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private VolunteerService volunteerService;
    @Autowired
    private ActivityApplyService activityApplyService;
    @Autowired
    private GroupService groupService;



    /**
     * 活动地区列表
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryAddressList")
    public DataResponseVo<Object> queryAddressList() {
        try{
            List<ActivityAddress> list = activityAddressService.selectAll();
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),list);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }
    /**
     * 新增活动地区
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/insertAddress")
    public DataResponseVo<Object> insertAddress(
                                                 @RequestParam("volunteerId") String  volunteerId,
                                                 @RequestParam("myprovince") String myprovince,
                                                 @RequestParam("mycity") String mycity,
                                                 @RequestParam("mydistrict") String mydistrict,
                                                 @RequestParam("myaddress") String myaddress,
                                                 @RequestParam("myaddressname") String myaddressname,
                                                 @RequestParam("myLng") String myLng,
                                                 @RequestParam("myLat") String myLat,
                                                 HttpServletRequest request) {
        try{
            Volunteer volunteer = volunteerService.selectByPrimaryKey(Long.valueOf(volunteerId));

            ActivityAddress activityAddress = new ActivityAddress();
            activityAddress.setMemberCode(volunteer.getMemberCode());
            activityAddress.setProvince(myprovince);
            activityAddress.setCity(mycity);
            activityAddress.setDistrict(mydistrict);
            activityAddress.setAddress(myaddress);
            activityAddress.setName(myaddressname);
            activityAddress.setDelStatus(0);
            activityAddress.setLat(Double.valueOf(myLat));
            activityAddress.setLng(Double.valueOf(myLng));
            activityAddress.setCreateTime(new Date());
            activityAddressService.insert(activityAddress);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


    @ResponseBody
    @RequestMapping(value = "/queryActivityList")
    public DataResponseVo<Object> queryActivityList(@RequestParam(value = "groupId") String  groupId
                                                    ,@RequestParam(value = "locked" ,required =false) String  locked
                                                    ,@RequestParam(value = "pageNo" ,required =false) String  pageNo){
        try{
            Activity activity = new Activity();
            activity.setGroupId(Long.valueOf(groupId));
            activity.setDelStatus(0);
            if(StrUtils.isNotEmpty(locked)){
                activity.setLocked(Integer.valueOf(locked));
            }
            if(StrUtils.isNotEmpty(pageNo)){
                activity.setLimitStart(Integer.valueOf(pageNo));
            }
            Page<Activity> page = activityService.selectByStatement(activity);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),page);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryActivityDetail")
    public DataResponseVo<Object> queryActivityDetail(@RequestParam(value = "id") String  id){
        try{
            Activity activity = activityService.selectByPrimaryKey(Long.valueOf(id));
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),activity);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }




    @ResponseBody
    @RequestMapping(value = "/insert")
    public DataResponseVo<Object> insert(@RequestParam("groupId") String  groupId,
                                         @RequestParam("volunteerId") String  volunteerId,
                                         @RequestParam("name") String  name,
                                         @RequestParam("demo") String demo,
                                         @RequestParam("startTime") String startTime,
                                         @RequestParam("endTime") String endTime,
                                         @RequestParam("tags") String tags,
                                         @RequestParam("needs") String needs,
                                         @RequestParam("scope") String scope,
                                         @RequestParam("addressId") String addressId,
                                         @RequestParam("myprovince") String myprovince,
                                         @RequestParam("mycity") String mycity,
                                         @RequestParam("mydistrict") String mydistrict,
                                         @RequestParam("myaddress") String myaddress,
                                         @RequestParam("myaddressname") String myaddressname,
                                         @RequestParam("myLng") String myLng,
                                         @RequestParam("myLat") String myLat,
                                         @RequestParam("monitorId") String monitorId,
                                         @RequestParam("skills") String skills,
                                         @RequestParam("pic") String pic,
                                         HttpServletRequest request) {
        try{
            Activity activity = new Activity();

            Volunteer volunteer = volunteerService.selectByPrimaryKey(Long.valueOf(volunteerId));

            //如果addressId =0 则是用户添加了新的活动地点，此时需要保存新的活动地点
            if(StrUtils.isEmpty(addressId) || Long.parseLong(addressId) ==0L) {
                //保存活动地点
                ActivityAddress activityAddress = new ActivityAddress();
                activityAddress.setMemberCode(volunteer.getMemberCode());
                activityAddress.setProvince(myprovince);
                activityAddress.setCity(mycity);
                activityAddress.setDistrict(mydistrict);
                activityAddress.setAddress(myaddress);
                activityAddress.setName(myaddressname);
                activityAddress.setDelStatus(0);
                activityAddress.setLat(Double.valueOf(myLat));
                activityAddress.setLng(Double.valueOf(myLng));
                activityAddress.setCreateTime(new Date());
                activityAddressService.insert(activityAddress);

                activity.setProvince(myprovince);
                activity.setCity(mycity);
                activity.setDistrict(mydistrict);
                activity.setAddress(myprovince + mycity + mydistrict + myaddress);
                activity.setLat(Double.parseDouble(myLat));
                activity.setLng(Double.parseDouble(myLng));
            }else {
                ActivityAddress address = activityAddressService.selectByPrimaryKey(Long.valueOf(addressId));
                activity.setProvince(address.getProvince());
                activity.setCity(address.getCity());
                activity.setDistrict(address.getDistrict());
                activity.setAddress(address.getAddress());
                activity.setLat(address.getLat());
                activity.setLng(address.getLng());
            }
            activity.setName(name);
            activity.setStartTime(startTime);
            activity.setEndTime(endTime);
            activity.setDelStatus(0);
            activity.setLocked(0);
            activity.setActStatus(1);
            activity.setCreateTime(new Date());
            activity.setApplys(0);
            activity.setAttends(0);
            activity.setImgs(pic);
            activity.setMonitor(Long.valueOf(monitorId));
            activity.setNeeds(Integer.valueOf(needs));
            if(StrUtils.isNotEmpty(scope)){
                activity.setScope(Integer.valueOf(scope));
            }
            activity.setGoodComments(0);
            activity.setMidComments(0);
            activity.setBadComments(0);
            activity.setDemo(demo);
            activity.setSelfGroup(0);
            //如果tags 为空则是用户自定义标签，此时需要保存用户标签
            if(StringUtils.isNotBlank(tags)){
                activity.setTags(tags);
            }
            activity.setTimes(0);
            activity.setTop(0);
            activity.setOldid(0l);
            //单次
            activity.setWeekDay("1");
            activity.setVolunteerId(Long.valueOf(volunteerId));
            activity.setBrowses(0);
            activity.setSkill(skills);
            activity.setClient(ConfigUtil.clientId);
            activity.setOrderNum(2);
//            Volunteer monitor = volunteerService.selectByPrimaryKey(Long.valueOf(monitorId));
            activity.setNeeds(Integer.parseInt(needs));
            activity.setScope(Integer.parseInt(scope));

            activity.setGroupId(Long.valueOf(groupId));
            Group group = groupService.selectByPrimaryKey(Long.valueOf(groupId));
            activity.setGroup(group);

            activityService.insert(activity);

            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }



    @ResponseBody
    @RequestMapping(value = "/update")
    public DataResponseVo<Object> update(@RequestParam("groupId") String  groupId,
                                         @RequestParam("volunteerId") String  volunteerId,
                                         @RequestParam("activityId") String  activityId,
                                         @RequestParam("name") String  name,
                                         @RequestParam("demo") String demo,
                                         @RequestParam("startTime") String startTime,
                                         @RequestParam("endTime") String endTime,
                                         @RequestParam("tags") String tags,
                                         @RequestParam("needs") String needs,
                                         @RequestParam("scope") String scope,
                                         @RequestParam(value = "addressId", required=true) String addressId,
                                         @RequestParam(value = "myprovince",required=false) String myprovince,
                                         @RequestParam(value = "mycity",required=false) String mycity,
                                         @RequestParam(value = "mydistrict",required=false) String mydistrict,
                                         @RequestParam(value = "myaddress",required=false) String myaddress,
                                         @RequestParam(value = "myaddressname",required=false) String myaddressname,
                                         @RequestParam("myLng") String myLng,
                                         @RequestParam("myLat") String myLat,
                                         @RequestParam("monitorId") String monitorId,
                                         @RequestParam("skills") String skills,
                                         @RequestParam("pic") String pic,
                                         HttpServletRequest request) {
        try{
            Activity activity = new Activity();
            Volunteer volunteer = volunteerService.selectByPrimaryKey(Long.valueOf(volunteerId));
            Group group = groupService.selectByPrimaryKey(Long.valueOf(groupId));

            if(StrUtils.isEmpty(addressId) || Long.parseLong(addressId) ==0L) {
                if(StrUtils.isEmpty(myprovince) || StrUtils.isEmpty(mycity) || StrUtils.isEmpty(mydistrict)|| StrUtils.isEmpty(myaddress)|| StrUtils.isEmpty(myaddressname)){

                }
                //保存活动地点
                ActivityAddress activityAddress = new ActivityAddress();
                activityAddress.setMemberCode(volunteer.getMemberCode());
                activityAddress.setProvince(myprovince);
                activityAddress.setCity(mycity);
                activityAddress.setDistrict(mydistrict);
                activityAddress.setAddress(myaddress);
                activityAddress.setName(myaddressname);
                activityAddress.setDelStatus(0);
                activityAddress.setLat(Double.valueOf(myLat));
                activityAddress.setLng(Double.valueOf(myLng));
                activityAddress.setCreateTime(new Date());
                activityAddressService.insert(activityAddress);

                activity.setProvince(myprovince);
                activity.setCity(mycity);
                activity.setDistrict(mydistrict);
                activity.setAddress(myprovince + mycity + mydistrict + myaddress);
                activity.setLat(Double.parseDouble(myLat));
                activity.setLng(Double.parseDouble(myLng));
            }else {
                ActivityAddress address = activityAddressService.selectByPrimaryKey(Long.valueOf(addressId));
                activity.setProvince(address.getProvince());
                activity.setCity(address.getCity());
                activity.setDistrict(address.getDistrict());
                activity.setAddress(address.getAddress());
                activity.setLat(address.getLat());
                activity.setLng(address.getLng());
            }
            activity.setId(Long.valueOf(activityId));
            activity.setName(name);
            activity.setStartTime(startTime);
            activity.setEndTime(endTime);
            activity.setGroup(group);
            activity.setMonitor(Long.valueOf(monitorId));
            activity.setDemo(demo);
            activity.setNeeds(Integer.valueOf(needs));
            activity.setScope(Integer.parseInt(scope));
            activity.setTags(tags);
            activity.setImgs(pic);
            activity.setSkill(skills);

            activityService.update(activity);
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }



    @ResponseBody
    @RequestMapping(value = "/delete")
        public DataResponseVo<Object> delete(@RequestParam("activityId")String activityId,@RequestParam("volunteerId")String volunteerId){
        try{
            Activity activity = activityService.selectByPrimaryKey(Long.valueOf(activityId));
            if(Long.valueOf(volunteerId)==activity.getVolunteerId() || Long.valueOf(volunteerId)==activity.getMonitor()){
                activityService.delete(Long.valueOf(activityId));
            }else{
                return new DataResponseVo<Object>(StatusCode.NO_ACCESS.getCode(),StatusCode.NO_ACCESS.getMessage());
            }
            return new DataResponseVo<Object>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo<Object>(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


}


















